package com.sxx.beautifulcity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.decoder.ProgressiveJpegConfig;
import com.facebook.imagepipeline.image.QualityInfo;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.application.ProjectApplication;
import com.sxx.beautifulcity.entity.greendao.AgritourismDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class AgritourismAdapter extends RecyclerView.Adapter<
        AgritourismAdapter.AgritourismRecyclerViewAdapterViewHolder> {
    //定义农家乐的集合
    private List<AgritourismDaoEntity> list;
    //定义行监听
    private OnMyItemClickListener listener;

    public  void  setOnMyItemClickListener(OnMyItemClickListener l){
        listener = l;
    }

    public AgritourismAdapter(Context c) {


    }

    /***
     * 添加数据
     * @param agritourisms
     */
    public void addData(List<AgritourismDaoEntity> agritourisms) {
        list = agritourisms;
        notifyDataSetChanged();//数据发生改变的时候提醒刷新数据
    }

    /***
     * 加载数据
     * @param agritourisms
     */
    public void loadData(List<AgritourismDaoEntity> agritourisms){
        this.list.addAll(agritourisms);
        notifyDataSetChanged();
    }

    class AgritourismRecyclerViewAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private LinearLayout agritourism_linearlayout;
        private TextView nameTv;
        private RatingBar ratingBar;
        private SimpleDraweeView draweeView;
        private int position;

        public AgritourismRecyclerViewAdapterViewHolder(View v) {
            super(v);

            nameTv = (TextView) v.findViewById(R.id.nameTv);
            ratingBar = (RatingBar) v.findViewById(R.id.ratingBar);
            draweeView = (SimpleDraweeView) v.findViewById(R.id.draweeView);
            agritourism_linearlayout = (LinearLayout) v.findViewById(R.id.agritourism_linearlayout);
            agritourism_linearlayout.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                AgritourismDaoEntity data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }

    /**
     * 加载农家院行布局
     * @param viewGroup
     * @param position
     * @return
     */
    @Override
    public AgritourismRecyclerViewAdapterViewHolder onCreateViewHolder(
            ViewGroup viewGroup, int position) {

        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.activity_agritourism_item, null);

        return new AgritourismRecyclerViewAdapterViewHolder(v);
    }

    /***
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(AgritourismRecyclerViewAdapterViewHolder holder, int position) {

        if (list != null && list.size() > 0) {
            AgritourismDaoEntity agritourism = list.get(position);
            holder.nameTv.setText(agritourism.getName());
            holder.ratingBar.setNumStars(agritourism.getStar());
            String path = agritourism.getImage();
            frescoGetImage(path, holder.draweeView);
        }
        //获得行布局所对应的下标
        holder.position = position;
    }

    /***
     * 使用Fresco图片三级缓存
     * @param path
     * @param draweeView
     */
    public void frescoGetImage(String path, SimpleDraweeView draweeView) {
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

//    public void getImage(String path , SimpleDraweeView draweeView){
//
//        final ControllerListener listener = new BaseControllerListener(){
//            //加载图片成功时回调
//            @Override
//            public void onFinalImageSet(String id, Object imageInfo, Animatable animatable) {
//                super.onFinalImageSet(id, imageInfo, animatable);
//
//            }
//
//            //加载图片失败时回调
//            @Override
//            public void onFailure(String id, Throwable throwable) {
//                super.onFailure(id, throwable);
//                throwable.printStackTrace();
//            }
//            //设置图片渐进式
//            @Override
//            public void onIntermediateImageFailed(String id, Throwable throwable) {
//                super.onIntermediateImageFailed(id, throwable);
//
//            }
//        };
//        DraweeController controller = Fresco.newDraweeControllerBuilder()
//                .setUri(path).setControllerListener(listener).build();
//        draweeView.setController(controller);
//   }

    public void getProgressFrescoImage(Context c) {

        ProgressiveJpegConfig config = new ProgressiveJpegConfig() {
            @Override
            public int getNextScanNumberToDecode(int scanNumber) {
                return 0;
            }

            @Override
            public QualityInfo getQualityInfo(int scanNumber) {
                return null;
            }
        };
        ImagePipelineConfig imagePipelineConfig = ImagePipelineConfig.
                newBuilder(c).setProgressiveJpegConfig(config).build();
        Fresco.initialize(ProjectApplication.getContext(), imagePipelineConfig);
    }

    @Override
    public int getItemCount() {

        return list != null && list.size() > 0 ? list.size() : 0;
    }

}
